//
//  THFlowEarnMyJoinVC.m
//  YJB
//
//  Created by 中电大维 on 2017/1/12.
//  Copyright © 2017年 YJB. All rights reserved.
//  我参与的

#import "THFlowEarnMyJoinVC.h"
#import "THFlowEarnMyJoinModel.h"
#import "THFlowEarnMyjoinCell.h"
#import "THFlowEarnCompleteDetaillModel.h"

@interface THFlowEarnMyJoinVC ()

@end

@implementation THFlowEarnMyJoinVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    if (self.navTitle.length) {
        self.title = self.navTitle;
    }
    
    self.tableView.tableFooterView = [UIView new];
    self.tableView.backgroundColor = [UIColor colorWithHexString:@"eeeeee"];
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadData)];
    [self.tableView.mj_header beginRefreshing];
    [self.tableView registerClass:[THFlowEarnMyjoinCell class] forCellReuseIdentifier:flowEarnMyJoinCellID];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    [self.tableView setSeparatorColor:[UIColor colorWithHexString:@"e5e5e5"]];
}

- (void)loadData
{
    if (self.navTitle.length) {//手动流量赚钱 (7)通过任务ID获取查看完成情况（查看完成详情列表）
        
        NSMutableDictionary *params = [NSMutableDictionary dictionary];
        NSString *userName = [YJUserDefaultTool readUserName];
        NSString *md5Key = [YJUserDefaultTool readKeyID];
        
        params[@"action"] = @"get_publish_info";
        params[@"makegold"] = @{@"task_id":self.taskID,
                                @"user_info":userName,
                                @"phone_info":@"iOS",
                                @"md5_key":md5Key};
        NSString *url = THURL(@"manual.php");
        
        [YJNetworkTool postWithUrl:url parameters:params success:^(id responseObject) {
            [self.tableView.mj_header endRefreshing];
            NSString *code = responseObject[@"return"][@"code"];
            if (code.integerValue == 200)
            {
                NSArray *array = responseObject[@"return"][@"data"][@"list"];
                self.tasks = [THFlowEarnCompleteDetaillModel mj_objectArrayWithKeyValuesArray:array];
            }
            else if(code.integerValue == 500)
                [self.tasks removeAllObjects];
            [self.tableView reloadData];
        } failure:^(NSError *error) {
            [MBProgressHUD showError:@"网络不佳，稍后重试" toView:self.view];
        }];

    }else{
#pragma mark - 全部的列表
        NSMutableDictionary *params = [NSMutableDictionary dictionary];
        NSString *userName = [YJUserDefaultTool readUserName];
        NSString *md5Key = [YJUserDefaultTool readKeyID];
        params[@"action"] = @"get_involve_list";
        params[@"makegold"] = @{@"username":userName,
                                @"firstrow":@"-1",
                                @"listrows":@"",
                                @"user_info":userName,
                                @"phone_info":@"iOS",
                                @"md5_key":md5Key};
        NSString *url = THURL(@"manual.php");
        
        [YJNetworkTool postWithUrl:url parameters:params success:^(id responseObject) {
            [self.tableView.mj_header endRefreshing];
            NSString *code = responseObject[@"return"][@"code"];
            if (code.integerValue == 200)
            {
                NSArray *array = responseObject[@"return"][@"data"][@"list"];
                self.tasks = [THFlowEarnMyJoinModel mj_objectArrayWithKeyValuesArray:array];
            }
            else if(code.integerValue == 500)
                [self.tasks removeAllObjects];
            [self.tableView reloadData];
        } failure:^(NSError *error) {
            [MBProgressHUD showError:@"网络不佳，稍后重试" toView:self.view];
            [self.tableView.mj_header endRefreshing];
        }];
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.tasks.count;
    
   // return 5;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    THFlowEarnMyjoinCell *cell = [tableView dequeueReusableCellWithIdentifier:flowEarnMyJoinCellID forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    if (self.navTitle.length) {
        cell.completeModel = self.tasks[indexPath.row];
    }else{
        cell.model = self.tasks[indexPath.row];
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
//    THFlowEarnMyJoinModel *model = self.tasks[indexPath.row];
//    return model.cellHeight;
    if (self.navTitle.length) {
        return 80;
    }else{
        return 107;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0;
}

@end
